package com.qc31.monitorlib.ui.listmonitor

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.qc31.monitorlib.R

/** @Author Jasper @Time 2020/11/30 @Desc 列表监控跳转弹窗 */
class ListItemPopup(
    context: Context,
    private val isCarMap: Boolean,
    private val clickListener: View.OnClickListener
) :
    PopupWindow() {
    private val tvLMPopTitle: TextView

    init {
        contentView = LayoutInflater.from(context).inflate(R.layout.popup_list_item, null)
        tvLMPopTitle = contentView.findViewById(R.id.tvLMPopTitle)
        initClick()

        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
    }

    private fun initClick() {
        contentView.findViewById<ImageView>(R.id.ivLMPopClose).setOnClickListener {
            dismiss()
        }

        val carMap = contentView.findViewById<TextView>(R.id.tvLMPopSingleCar)
        if (isCarMap) {
            carMap.setOnClickListener(clickListener)
        } else {
            carMap.visibility = View.GONE
        }

        contentView.findViewById<TextView>(R.id.tvLMPopLine).setOnClickListener(clickListener)
        contentView.findViewById<TextView>(R.id.tvLMPopInfo).setOnClickListener(clickListener)
        contentView.findViewById<TextView>(R.id.tvLMPopMile).setOnClickListener(clickListener)
        contentView.findViewById<TextView>(R.id.tvLMPopAlarm).setOnClickListener(clickListener)
        contentView.findViewById<TextView>(R.id.tvLMPopPhoto).setOnClickListener(clickListener)
        contentView.findViewById<TextView>(R.id.tvLMPopStopCar).setOnClickListener(clickListener)
    }

    fun showAsDropDown(anchor: View?, title: String) {
        super.showAsDropDown(anchor)
        tvLMPopTitle.text = title
    }

    fun showAtLocation(anchor: View?, title: String) {
        super.showAtLocation(anchor, Gravity.CENTER, 0, 0)
        tvLMPopTitle.text = title
    }

}